home *** CD-ROM | disk | FTP | other *** search
/ Champak 106 / Vol 106.iso / games / rain_for.swf / scripts / __Packages / smashing / Line.as < prev    next >
Encoding:
Text File  |  2010-04-12  |  11.5 KB  |  355 lines

  1. class smashing.Line
  2. {
  3.    var p0;
  4.    var p1;
  5.    var vector;
  6.    var lineNormal;
  7.    var vN;
  8.    var nLength;
  9.    var normal;
  10.    var bChanged = true;
  11.    function Line(x0, y0, x1, y1)
  12.    {
  13.       this.p0 = new smashing.Point(x0,y0);
  14.       this.p1 = new smashing.Point(x1,y1);
  15.       this.vector = new smashing.Point(this.p1.x - this.p0.x,this.p1.y - this.p0.y);
  16.       this.lineNormal = this.vector.normalize();
  17.       this.vN = new smashing.Point(this.vector.y,- this.vector.x);
  18.       this.vN.normalizeMe();
  19.       this.nLength = this.vector.length;
  20.    }
  21.    function get normal()
  22.    {
  23.       return this.vN;
  24.    }
  25.    function setLine(x0, y0, x1, y1)
  26.    {
  27.       this.p0.x = x0;
  28.       this.p0.y = y0;
  29.       this.p1.x = x1;
  30.       this.p1.y = y1;
  31.       this.vector.x = this.p1.x - this.p0.x;
  32.       this.vector.y = this.p1.y - this.p0.y;
  33.       this.lineNormal = this.vector.normalize();
  34.       this.vN = new smashing.Point(this.vector.y,- this.vector.x);
  35.       this.vN.normalizeMe();
  36.       this.nLength = this.vector.length;
  37.       this.bChanged = true;
  38.    }
  39.    function move(nX, nY)
  40.    {
  41.       this.p0.x += nX;
  42.       this.p0.y += nY;
  43.       this.p1.x += nX;
  44.       this.p1.y += nY;
  45.    }
  46.    function collisionPointSwept(p, m, radius)
  47.    {
  48.       var _loc26_ = m.dot(this.normal);
  49.       if(_loc26_ >= 0)
  50.       {
  51.          return null;
  52.       }
  53.       var _loc19_ = new smashing.Point(p.x - this.p0.x,p.y - this.p0.y);
  54.       var _loc9_ = this.normal.copy();
  55.       _loc9_.multiplyMe(radius);
  56.       var _loc4_ = _loc19_.copy();
  57.       _loc4_.subtractPointMe(_loc9_);
  58.       var _loc6_ = ((- _loc4_.y) * m.x + _loc4_.x * m.y) / (m.y * this.vector.x + (- m.x) * this.vector.y);
  59.       var _loc5_ = undefined;
  60.       if(_loc6_ >= 0 && _loc6_ <= 1)
  61.       {
  62.          var _loc3_ = new smashing.Point(this.vector.x * _loc6_,this.vector.y * _loc6_);
  63.          var _loc8_ = _loc3_.x - _loc4_.x;
  64.          var _loc7_ = _loc3_.y - _loc4_.y;
  65.          var _loc13_ = m.x;
  66.          var _loc12_ = m.y;
  67.          var _loc28_ = new smashing.Point(_loc3_.x - _loc4_.x,_loc3_.y - _loc4_.y);
  68.          if(_loc8_ * _loc13_ + _loc7_ * _loc12_ < 0)
  69.          {
  70.             return null;
  71.          }
  72.          _loc8_ *= _loc8_;
  73.          _loc7_ *= _loc7_;
  74.          _loc13_ *= _loc13_;
  75.          _loc12_ *= _loc12_;
  76.          var _loc25_ = _loc8_ + _loc7_;
  77.          if(_loc25_ > _loc13_ + _loc12_)
  78.          {
  79.             return null;
  80.          }
  81.          _loc3_.x += this.p0.x + _loc9_.x;
  82.          _loc3_.y += this.p0.y + _loc9_.y;
  83.          _loc5_ = new smashing.CollisionResult(_loc3_,this.normal,_loc25_);
  84.          _loc5_.gizmo = this;
  85.          _loc5_.lHit = _loc3_;
  86.       }
  87.       else
  88.       {
  89.          var _loc18_ = m.normalize();
  90.          if(_loc6_ < 0)
  91.          {
  92.             var _loc23_ = this.p0;
  93.             var _loc20_ = _loc19_.reverse();
  94.          }
  95.          else if(_loc6_ > 0)
  96.          {
  97.             _loc23_ = this.p1;
  98.             _loc20_ = new smashing.Point(this.p1.x - p.x,this.p1.y - p.y);
  99.          }
  100.          var _loc14_ = _loc20_.dot(_loc18_);
  101.          if(m.length < Math.abs(_loc14_) - radius)
  102.          {
  103.             return null;
  104.          }
  105.          var _loc17_ = _loc20_.length;
  106.          var _loc24_ = _loc17_ * _loc17_ - _loc14_ * _loc14_;
  107.          var _loc21_ = radius * radius;
  108.          if(_loc24_ > _loc21_)
  109.          {
  110.             return null;
  111.          }
  112.          var _loc10_ = Math.sqrt(_loc21_ - _loc24_) - _loc14_;
  113.          if(Math.abs(_loc10_) > m.length)
  114.          {
  115.             return null;
  116.          }
  117.          var _loc15_ = _loc18_.multiply(_loc10_);
  118.          if(_loc15_.dot(m) > 0)
  119.          {
  120.             return null;
  121.          }
  122.          var _loc16_ = new smashing.Point(p.x - _loc15_.x,p.y - _loc15_.y);
  123.          var _loc27_ = new smashing.Point(_loc16_.x - _loc23_.x,_loc16_.y - _loc23_.y).normalize();
  124.          _loc5_ = new smashing.CollisionResult(_loc16_,_loc27_,_loc10_ * _loc10_);
  125.          _loc5_.gizmo = this;
  126.          _loc5_.lHit = _loc15_;
  127.       }
  128.       return _loc5_;
  129.    }
  130.    function collisionPoint(p, m, r)
  131.    {
  132.       var _loc18_ = m.dot(this.normal);
  133.       if(_loc18_ > 0)
  134.       {
  135.          return null;
  136.       }
  137.       var _loc3_ = new smashing.Point(p.x + m.x,p.y + m.y);
  138.       var _loc12_ = this.vector.x * (_loc3_.x - this.p0.x) + this.vector.y * (_loc3_.y - this.p0.y);
  139.       var _loc13_ = this.vector.dot(this.vector);
  140.       var _loc2_ = undefined;
  141.       var _loc9_ = undefined;
  142.       var _loc7_ = undefined;
  143.       if(_loc12_ < 0)
  144.       {
  145.          _loc2_ = new smashing.Point(_loc3_.x - this.p0.x,_loc3_.y - this.p0.y);
  146.          _loc9_ = _loc2_.dot(_loc2_);
  147.          if(_loc9_ > r * r)
  148.          {
  149.             return null;
  150.          }
  151.          var _loc11_ = _loc2_.normalize();
  152.          _loc2_.length = r;
  153.          var _loc5_ = this.p0.addPoint(_loc2_);
  154.          var _loc14_ = _loc5_.x - p.x;
  155.          var _loc16_ = _loc5_.y - p.y;
  156.          _loc7_ = new smashing.CollisionResult(_loc5_,_loc11_,_loc14_ * _loc14_ + _loc16_ * _loc16_);
  157.          _loc7_.lHit = this.p0;
  158.          _loc7_.nRatio = 0;
  159.          _loc7_.gizmo = this;
  160.       }
  161.       else if(_loc12_ > _loc13_)
  162.       {
  163.          _loc2_ = new smashing.Point(_loc3_.x - this.p1.x,_loc3_.y - this.p1.y);
  164.          _loc9_ = _loc2_.dot(_loc2_);
  165.          if(_loc9_ > r * r)
  166.          {
  167.             return null;
  168.          }
  169.          _loc11_ = _loc2_.normalize();
  170.          _loc2_.length = r;
  171.          _loc5_ = this.p1.addPoint(_loc2_);
  172.          _loc14_ = _loc5_.x - p.x;
  173.          _loc16_ = _loc5_.y - p.y;
  174.          _loc7_ = new smashing.CollisionResult(_loc5_,_loc11_,_loc14_ * _loc14_ + _loc16_ * _loc16_);
  175.          _loc7_.lHit = this.p1;
  176.          _loc7_.nRatio = 1;
  177.          _loc7_.gizmo = this;
  178.       }
  179.       else
  180.       {
  181.          var _loc10_ = _loc12_ / _loc13_;
  182.          var _loc8_ = new smashing.Point(this.p0.x + this.vector.x * _loc10_,this.p0.y + this.vector.y * _loc10_);
  183.          var _loc17_ = _loc3_.x - _loc8_.x;
  184.          var _loc15_ = _loc3_.y - _loc8_.y;
  185.          if(_loc17_ * _loc17_ + _loc15_ * _loc15_ > r * r)
  186.          {
  187.             return null;
  188.          }
  189.          _loc5_ = new smashing.Point(_loc8_.x + this.normal.x * r,_loc8_.y + this.normal.y * r);
  190.          _loc14_ = _loc5_.x - p.x;
  191.          _loc16_ = _loc5_.y - p.y;
  192.          _loc7_ = new smashing.Line(_loc5_.x + _loc11_.x,_loc5_.y + _loc11_.y,_loc5_.x,_loc5_.y);
  193.          var _loc19_ = _loc14_ * _loc14_ + _loc16_ * _loc16_;
  194.          _loc7_ = new smashing.CollisionResult(_loc5_,this.normal,_loc19_);
  195.          _loc7_.lHit = _loc8_;
  196.          _loc7_.nRatio = _loc10_;
  197.          _loc7_.gizmo = this;
  198.       }
  199.       return _loc7_;
  200.    }
  201.    function collisionPointThresh(p, m, r)
  202.    {
  203.       var _loc16_ = m.dot(this.normal);
  204.       if(_loc16_ > 0)
  205.       {
  206.          return null;
  207.       }
  208.       var _loc12_ = new smashing.Point(p.x + m.x,p.y + m.y);
  209.       var _loc2_ = new smashing.Point(_loc12_.x - this.p0.x,_loc12_.y - this.p0.y);
  210.       var _loc13_ = _loc2_.dot(this.vN);
  211.       if(Math.abs(_loc13_) > r)
  212.       {
  213.          return null;
  214.       }
  215.       if(_loc13_ < r / 2)
  216.       {
  217.          return null;
  218.       }
  219.       var _loc10_ = _loc2_.dot(this.lineNormal);
  220.       var _loc7_ = undefined;
  221.       var _loc6_ = undefined;
  222.       if(_loc10_ < 0)
  223.       {
  224.          _loc7_ = _loc2_.dot(_loc2_);
  225.          if(_loc7_ > r * r)
  226.          {
  227.             return null;
  228.          }
  229.          var _loc9_ = _loc2_.normalize();
  230.          _loc2_.length = r;
  231.          var _loc4_ = this.p0.addPoint(_loc2_);
  232.          var _loc14_ = _loc4_.x - p.x;
  233.          var _loc15_ = _loc4_.y - p.y;
  234.          _loc6_ = new smashing.CollisionResult(_loc4_,_loc9_,_loc14_ * _loc14_ + _loc15_ * _loc15_);
  235.          _loc6_.lHit = this.p0;
  236.          _loc6_.nRatio = 0;
  237.          _loc6_.gizmo = this;
  238.       }
  239.       else if(_loc10_ > this.nLength)
  240.       {
  241.          _loc7_ = _loc2_.dot(_loc2_);
  242.          if(_loc7_ > r * r)
  243.          {
  244.             return null;
  245.          }
  246.          _loc9_ = _loc2_.normalize();
  247.          _loc2_.length = r;
  248.          _loc4_ = this.p1.addPoint(_loc2_);
  249.          _loc14_ = _loc4_.x - p.x;
  250.          _loc15_ = _loc4_.y - p.y;
  251.          _loc6_ = new smashing.CollisionResult(_loc4_,_loc9_,_loc14_ * _loc14_ + _loc15_ * _loc15_);
  252.          _loc6_.lHit = this.p1;
  253.          _loc6_.nRatio = 1;
  254.          _loc6_.gizmo = this;
  255.       }
  256.       else
  257.       {
  258.          var _loc8_ = _loc10_ / this.nLength;
  259.          var _loc11_ = new smashing.Point(this.p0.x + this.vector.x * _loc8_,this.p0.y + this.vector.y * _loc8_);
  260.          _loc4_ = new smashing.Point(_loc11_.x + this.normal.x * r,_loc11_.y + this.normal.y * r);
  261.          _loc14_ = _loc4_.x - p.x;
  262.          _loc15_ = _loc4_.y - p.y;
  263.          _loc6_ = new smashing.Line(_loc4_.x + _loc9_.x,_loc4_.y + _loc9_.y,_loc4_.x,_loc4_.y);
  264.          var _loc17_ = _loc14_ * _loc14_ + _loc15_ * _loc15_;
  265.          _loc6_ = new smashing.CollisionResult(_loc4_,this.normal,_loc17_);
  266.          _loc6_.lHit = _loc11_;
  267.          _loc6_.nRatio = _loc8_;
  268.          _loc6_.gizmo = this;
  269.       }
  270.       return _loc6_;
  271.    }
  272.    function nearestPointSeg(m0)
  273.    {
  274.       var _loc5_ = new smashing.Point(m0.x - this.p0.x,m0.y - this.p0.y);
  275.       var _loc2_ = this.vector.dot(_loc5_);
  276.       var _loc4_ = this.vector.dot(this.vector);
  277.       if(_loc2_ < 0)
  278.       {
  279.          return this.p0;
  280.       }
  281.       if(_loc2_ > _loc4_)
  282.       {
  283.          return this.p1;
  284.       }
  285.       var _loc3_ = _loc2_ / _loc4_;
  286.       return new smashing.Point(this.p0.x + this.vector.x * _loc3_,this.p0.y + this.vector.y * _loc3_);
  287.    }
  288.    function nearestPointLine(m0)
  289.    {
  290.       var _loc5_ = new smashing.Point(m0.x - this.p0.x,m0.y - this.p0.y);
  291.       var _loc3_ = this.vector.dot(_loc5_);
  292.       var _loc4_ = this.vector.dot(this.vector);
  293.       var _loc2_ = _loc3_ / _loc4_;
  294.       return new smashing.Point(this.p0.x + this.vector.x * _loc2_,this.p0.y + this.vector.y * _loc2_);
  295.    }
  296.    function nearestPointRatio(p)
  297.    {
  298.       var _loc5_ = new smashing.Point(p.x - this.p0.x,p.y - this.p0.y);
  299.       var _loc3_ = this.vector.dot(_loc5_);
  300.       var _loc4_ = this.vector.dot(this.vector);
  301.       var _loc2_ = _loc3_ / _loc4_;
  302.       return _loc2_;
  303.    }
  304.    function distanceFrom(p)
  305.    {
  306.       var _loc3_ = new smashing.Point(p.x - this.p0.x,p.y - this.p0.y);
  307.       var _loc2_ = this.normal.dot(_loc3_);
  308.       return _loc2_;
  309.    }
  310.    function intersection(op0, op1, oVector)
  311.    {
  312.       var _loc11_ = undefined;
  313.       var _loc9_ = undefined;
  314.       var _loc7_ = undefined;
  315.       var _loc10_ = undefined;
  316.       var _loc8_ = undefined;
  317.       var _loc6_ = undefined;
  318.       var _loc5_ = undefined;
  319.       var _loc4_ = undefined;
  320.       var _loc3_ = undefined;
  321.       var _loc12_ = undefined;
  322.       var _loc13_ = undefined;
  323.       if(this.vector.x != 0)
  324.       {
  325.          _loc4_ = this.vector.y / this.vector.x;
  326.       }
  327.       else
  328.       {
  329.          _loc4_ = Infinity;
  330.       }
  331.       if(op1.x - op0.x != 0)
  332.       {
  333.          _loc3_ = (op1.y - op0.y) / (op1.x - op0.x);
  334.       }
  335.       else
  336.       {
  337.          _loc3_ = Infinity;
  338.       }
  339.       _loc11_ = _loc4_;
  340.       _loc10_ = _loc3_;
  341.       _loc9_ = -1;
  342.       _loc8_ = -1;
  343.       _loc7_ = this.p0.y - _loc4_ * this.p0.x;
  344.       _loc6_ = op0.y - _loc3_ * op0.x;
  345.       _loc5_ = 1 / (_loc11_ * _loc8_ - _loc10_ * _loc9_);
  346.       _loc12_ = (_loc9_ * _loc6_ - _loc8_ * _loc7_) * _loc5_;
  347.       _loc13_ = (_loc10_ * _loc7_ - _loc11_ * _loc6_) * _loc5_;
  348.       return new smashing.Point(_loc12_,_loc13_);
  349.    }
  350.    function toString()
  351.    {
  352.       return "Line(" + this.p0.x + ", " + this.p0.y + ", " + this.p1.x + ", " + this.p1.y + ")";
  353.    }
  354. }
  355.